package structures.kys;

import java.io.Serializable;
import java.util.List;

import constants.Globals;

import manager.LoadNativeTableManager;
import manager.SessionManager;

import domain.DadServiceParametersDAO;

import exception.IBToolsException;

import structures.ValuePair;
import structures.xmltable.ServiceBaseTypeParameter;
import structures.xmltable.ServiceTableColumn;

public class KysServiceParameter implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3737368191633794441L;

	int baseParameterBagIndex;
	int parentParameterBagIndex;
	
	private boolean array;
	private String businessType;
	private String description;
	private String domain;
	private String domainType;
	private long serviceOid;
	private boolean input;
	private boolean optional;
	private String name;
	private String type;
	private DadServiceParametersDAO parent;
	private Long parameterOid;

	private String valueTable;
	private String valueColumn;
	
	
	public KysServiceParameter() {
		super();
		parent = new DadServiceParametersDAO();
	}

	
	public KysServiceParameter(ServiceTableColumn tableColumn) {
		super();
		this.setBaseParameterBagIndex(-1);
		this.array = false;
		this.businessType = "";
		this.description = tableColumn.getColumnName();
		this.domain = "";
		this.domainType = "";
		this.input = true;
		this.optional = true;
		this.name = tableColumn.getColumnName();
		this.type = Globals.PARAM_STRING;
		this.parent = null;
		this.resetParentOid(tableColumn);
	}

	public KysServiceParameter(ServiceBaseTypeParameter baseTypeParameter) {
		super();
		this.setBaseParameterBagIndex(baseTypeParameter.getBagIndex());
		this.array = false;
		this.businessType = "";
		this.description = baseTypeParameter.getAciklama();
		this.domain = "";
		this.domainType = "";
		this.input = baseTypeParameter.isInput();
		this.optional = baseTypeParameter.isOptional();
		this.name = baseTypeParameter.getBagKey();
		this.type = baseTypeParameter.getType();
		this.parent = null;
		if(baseTypeParameter.getDbParameterOid() > 0)
			this.parameterOid = baseTypeParameter.getDbParameterOid();
	}
	
	public void loadIntoBaseTypeParameter(ServiceBaseTypeParameter baseTypeParameter) throws IBToolsException{
		baseTypeParameter.setAciklama(description);
		baseTypeParameter.setBagKey(name);
		baseTypeParameter.setOptional(optional);
		baseTypeParameter.setInput(input);
		baseTypeParameter.setType(type);
		baseTypeParameter.fillPossibleValues();
	}
	
	public boolean isArray() {
		return array;
	}
	public void setArray(boolean isArray) {
		this.array = isArray;
	}
	public String getBusinessType() {
		return businessType;
	}
	public void setBusinessType(String businessType) {
		this.businessType = businessType;
	}
	public String getDescription() {
		return description;
	}
	public String fullDescription() throws IBToolsException {
		
		if(description.indexOf(SessionManager.getLoginUserCode()) < 0)
			description = "IBT(" + SessionManager.getLoginUserCode() + ") - " + (description == null || description.equals("") ? name : description + " ") ;
		
		if(valueTable != ""){
			if(valueColumn == ""){
				ValuePair columnPair = LoadNativeTableManager.analyzeCodeDescriptionColumns(valueTable);
				if(!columnPair.getLabel().equalsIgnoreCase(Globals.EMPTY_STRING)
						&& !columnPair.getValue().equalsIgnoreCase(Globals.EMPTY_STRING)){
					valueColumn = columnPair.getValue() + "," + columnPair.getLabel();
				}
			}
			if(valueColumn != ""){
				description = description + "P(" + valueTable + ")P C(" + valueColumn + ")C";
			}
			
		}
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getDomain() {
		return domain;
	}
	public void setDomain(String domain) {
		this.domain = domain;
	}
	public String getDomainType() {
		return domainType;
	}
	public void setDomainType(String domainType) {
		this.domainType = domainType;
	}
	public long getServiceOid() {
		return serviceOid;
	}
	public void setServiceOid(long serviceOid) {
		this.serviceOid = serviceOid;
	}
	public boolean isInput() {
		return input;
	}
	public void setInput(boolean isInput) {
		this.input = isInput;
	}
	public boolean isOptional() {
		return optional;
	}
	public void setOptional(boolean isOptional) {
		this.optional = isOptional;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public Long getParameterOid() {
		return parameterOid;
	}
	public void setParameterOid(Long parameterOid) {
		this.parameterOid = parameterOid;
	}
	
	public String getIoType(){
		return input ? "I" : "O";
	}
	public void setIoType(String type){
		input = type.equals("I");
	}
	
	public String getOptionalStr(){
		return optional ? "O" : "M";
	}
	public void setOptionalStr(String opt){
		optional = opt.equals("O");
	}

	public int getBaseParameterBagIndex() {
		return baseParameterBagIndex;
	}

	public void setBaseParameterBagIndex(int baseParameterBagIndex) {
		this.baseParameterBagIndex = baseParameterBagIndex;
	}

	public String getValueTable() {
		return valueTable;
	}

	public void setValueTable(String valueTable) {
		this.valueTable = valueTable;
	}

	public String getValueColumn() {
		return valueColumn;
	}

	public void setValueColumn(String valueColumn) {
		this.valueColumn = valueColumn;
	}

	public void setParentName(String name) {
		
	}
	public String getParentName() {
		if(parent == null)
			return "";
		return parent.getName();
	}

	public DadServiceParametersDAO getParent() {
		if(parent == null)
			parent = new DadServiceParametersDAO();
		return parent;
	}

	public void setParent(DadServiceParametersDAO parent) {
		this.parent = parent;
	}


	public void resetParentOid(ServiceTableColumn parameter) {
		if(parent == null)
			parent = new DadServiceParametersDAO();
		parent.setOid(parameter.getParentOid());
		parent.setName(parameter.retrieveParentParameterName());
	}
	
	public int getParentParameterBagIndex() {
		return parentParameterBagIndex;
	}

	public void setParentParameterBagIndex(int parentParameterBagIndex) {
		this.parentParameterBagIndex = parentParameterBagIndex;
	}
	
	public boolean isNewParameter(){
		return (parameterOid == null || parameterOid < 1);
	}

	private boolean oneTablePresent;
	public void putOneTablePresentInfo(boolean oneTablePresentInfo){
		oneTablePresent = oneTablePresentInfo;
	}
	public boolean isSingleParentDisplayed(){
		return (oneTablePresent || 
				(type != null && type.equalsIgnoreCase(Globals.PARAM_TABLE) && (parent == null || parent.getOid() == null || parent.getOid() < 1)));
	}

}
